import request, { get, post, put, del } from '@/utils/request'

const defaultApi = (apiName) => {
  return {
    list: (params) => get(`/${apiName}/list`, params),
    page: (params) => get(`/${apiName}`, params),
    read: (id) => get(`/${apiName}/` + id),
    create: (id, data) => post(`/${apiName}`, {}, data),
    update: (id, data) => put(`/${apiName}/` + id, {}, { ...data, create_time: undefined, id: undefined }),
    delete: (id) => del(`/${apiName}/` + id)
  }
}
export const http = {
  user: defaultApi('user'),
  contact: defaultApi('contact'),
  banner: defaultApi('banner'),
  ship: defaultApi('ship'),
  order: {
    ...defaultApi('order'),
    cancel: (orderId) => post('/order/cancel', {}, { id: orderId }),
    sendEmail: (orderId) => post('/order/sendEmail', {}, { id: orderId })
  },
  orderCompany: defaultApi('orderCompany'),
  cycle: {
    ...defaultApi('cycle'),
    batchCreate: (id, data) => post('/cycle/batchCreate', {}, data)
  },
  coupon: defaultApi('coupon'),
  payment: {
    paypalRefund: (data) => post('/payment/paypal/refund', {}, data),
    alipayRefund: data => post('/payment/alipay/refund', {}, data)
  },
  gallery: defaultApi('gallery'),
  apiLog: defaultApi('apiLog'),
  statistics: {
    dashboard: (params) => get('/dashboard', params)
  },
  emailTemplate: defaultApi('emailTemplate'),
  email: defaultApi('email')
}

export const uploadFile = (file) => {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/upload/file',
    method: 'post',
    data: formData
  })
}
